package com.polaris.live.utils.zego.listener

import android.animation.Animator
import android.animation.ValueAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import im.zego.zegoexpress.constants.ZegoPlayerState
import im.zego.zegoexpress.constants.ZegoRemoteDeviceState
import org.json.JSONObject

/**
 * OnLivePlayViewAlphaImpl  辅助view展示回调
 * view 透明度改变
 * @author Created by 天晴 on 2024/1/27 15:04
 * @since 1.0.0
 **/
class OnLivePlayViewAlphaImpl(
    private val view: View,
    private val anim: Boolean = false,
    private val duration: Long = 500
) : OnLivePlayListenerImpl(){

    private var onPlayerRenderVideoFirstFrame = false
    private var mValueAnimator: ValueAnimator? = null
    override fun onPlayStop() {
        if (anim) {
            animHideLive()
        } else {
            view.visibility = View.GONE
        }

    }

    override fun onPlayerRenderVideoFirstFrame() {
        onPlayerRenderVideoFirstFrame = true
        if (anim) {
            animShowLive()
        } else {
            view.visibility = View.VISIBLE
        }
    }

    override fun onPlayerStateUpdate(
        state: ZegoPlayerState?,
        errorCode: Int,
        extendedData: JSONObject?
    ) {
        if (state == ZegoPlayerState.NO_PLAY && errorCode != 0) { //拉流失败
            if (anim) {
                animHideLive()
            } else {
                view.visibility = View.GONE
            }
        } else if (state == ZegoPlayerState.PLAY_REQUESTING && errorCode == 0) { //重试拉流

        }
    }

    override fun onRemoteSpeakerStateUpdate(state: ZegoRemoteDeviceState) {
        if (state == ZegoRemoteDeviceState.OPEN) {
            if (onPlayerRenderVideoFirstFrame) {

                if (anim) {
                    animShowLive()
                } else {
                    view.visibility = View.VISIBLE
                }
            }
        } else {
            if (anim) {
                animHideLive()
            } else {
                view.visibility = View.GONE
            }
        }
    }

    private fun animShowLive() {  //动画开启直播界面
        mValueAnimator?.takeIf { it.isRunning }?.apply {
            cancel()
            mValueAnimator = null
        }
        mValueAnimator = ValueAnimator.ofFloat(0f, 1f).apply {
            interpolator = LinearInterpolator()
            duration = this@OnLivePlayViewAlphaImpl.duration
            repeatCount = 0
            addUpdateListener { animation ->
                val value = animation.animatedValue as Float
                view.alpha = value
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                    view.visibility = View.VISIBLE
                }

                override fun onAnimationEnd(animation: Animator) {
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })
            start()
        }
    }

    private fun animHideLive() { //动画关闭直播界面
        mValueAnimator?.takeIf { it.isRunning }?.apply {
            cancel()
            mValueAnimator = null
        }
        mValueAnimator = ValueAnimator.ofFloat(1f, 0f).apply {
            interpolator = LinearInterpolator()
            duration = this@OnLivePlayViewAlphaImpl.duration
            repeatCount = 0
            addUpdateListener { animation ->
                val value = animation.animatedValue as Float
                view.alpha = value
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                }

                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.GONE
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })
            start()
        }
    }
}
